home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 691 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.3 KB  |  97 lines

  1. Path: engnews2.Eng.Sun.COM!usenet
  2. From: nitin@more.eng.sun.com (Nitin More [CONTRACTOR])
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help: How to initialize objects created by new class[x]
  5. Date: 05 Jan 1996 21:08:53 GMT
  6. Organization: SunSoft
  7. Message-ID: <NITIN.96Jan5130853@more.eng.sun.com>
  8. References: <4cicd6$lsv@news.capitalnet.com>
  9.     <30ECFA47.446B9B3D@intellektik.informatik.th-darmstadt.de>
  10. NNTP-Posting-Host: more.eng.sun.com
  11. In-reply-to: Enno Sandner's message of Fri, 05 Jan 1996 11:15:35 +0100
  12.  
  13. In article <30ECFA47.446B9B3D@intellektik.informatik.th-darmstadt.de> Enno Sandner <enno@intellektik.informatik.th-darmstadt.de> writes:
  14.  
  15. $  From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  16. $  Newsgroups: comp.lang.c++
  17. $  Date: Fri, 05 Jan 1996 11:15:35 +0100
  18. $  Organization: Fachbereich Informatik, TH Darmstadt
  19.  
  20. $  wingl@capitalnet.com wrote:
  21. $  > 
  22. $  > Let say I have the following class
  23. $  > 
  24. $  > My_class {
  25. $  >   public:
  26. $  >     int (int i=0) : a(i), b(i) {}
  27. $  >   private:
  28. $  >     int a, b;
  29. $  > }
  30. $  > 
  31. $  > For the following statement, default constructor is involved upon
  32. $  > the creation of the object.
  33. $  > 
  34. $  >            My_class* my_ptr= new My_class;
  35. $  > 
  36. $  > However,  if I use
  37. $  >                          My_class* my_ptr= new My_class[10];
  38. $  > 
  39. $  > to allocate 10 objects of class My_class, then C++ would not
  40. $  > call any constructor to do any initialization.  All the objects
  41. $  > allocated are not initializated at all.
  42. $  > 
  43. $  > I wonder what is the proper way to perform object initialization
  44. $  > whenever an array of objects are created?  As long as a constructor
  45. $  > is involved for each of the object in the array, I don't care if all the
  46. $  > objects are initializated the same or not.
  47. $  > 
  48. $  > If there is no way to get constructor to initialize an array of objects
  49. $  > upon creation.  Does that means one should not do so?
  50. $  > 
  51.  
  52. $  That's not true. The expression 'new My_class[10]' should call the 
  53. $  default-ctor of 'My_class' for each element.
  54. $  (I will assume the class looks like
  55. $
  56. $           class My_class {
  57. $             public:
  58. $               My_class (int i=0) : a(i), b(i) {}
  59. $             private:
  60. $               int a, b;
  61. $           };
  62. $
  63. $  ).
  64. $  The problems start when your class doesn't provide a default-ctor
  65. $  or you want to initialize elements of the array with different values.
  66. $  Here you are out of luck, because C++ doesn't provide an appropriate
  67. $  syntax. You'll have to use some hack like using the placement-new/delete
  68. $  operators.
  69.  
  70. $       Enno
  71.  
  72. You can define your class as follows:
  73.  
  74.            class My_class {
  75.              public:
  76.                My_class () { init(); }
  77.                My_class (int i) { init(i); }
  78.              private:
  79.                int a, b;
  80.                void init(int i=0) { a = i; b = i; }
  81.            };
  82.  
  83. In other words, 
  84. (1) define an init method.
  85. (2) define a default constructor to call init.
  86. (3) Remove default argument from your original constructor so that the compiler
  87.     can distinguish between the default and the other constructor.
  88. (4) Call the new init method from your original constructor.
  89.  
  90. -Nitin
  91. -- 
  92. ----------------------------------------------------------------------
  93. Nitin More                                                         
  94. SunSoft, Bldg 16  Off: (415) 786 7109                                 
  95. Menlo Park, CA    Fax: (415) 786 7957   e-mail: nitin@more.eng.sun.com
  96. ----------------------------------------------------------------------
  97.